﻿CREATE FUNCTION [dbo].[GetNextFuelSequence]
(
    @sequenceOrdVal INT, 
    @sequenceId BIGINT 
) RETURNS VARCHAR(17) 
AS 
BEGIN 
    DECLARE @date VARCHAR(4) 
    SET @date = CAST(@sequenceOrdVal AS VARCHAR(4)) 
 
    DECLARE @number BIGINT 
    SELECT 
        @number = COALESCE(MAX(aux.id) - MIN(aux.id) + 2, 1) 
    FROM 
        FuelTableSeq aux 
    WHERE 
        aux.ordval = @sequenceOrdVal
        AND aux.id < @sequenceId 
 
    DECLARE @result VARCHAR(17) 
    --SET @result = @date + '_' + RIGHT('00000000' + CAST(@number AS VARCHAR(8)), 8) 
	SET @result = RIGHT('00000000' + CAST(@number AS VARCHAR(8)), 8) 
    RETURN @result 
END 